import { Service } from 'egg';

class FriendsService extends Service {
    /**
     * 查找好友数量
     * @param param 
     */
    public async searchFriends(param: any) {
        console.log(param);
        const type = Number(param.type);
        let queryStr: string = ``;
        if (type === 1) {
            queryStr = `
                SELECT
                    u.user_id,
                    u.user_nickname,
                    u.user_avatarUrl,
                    u.user_sex,
                    u.user_sound_url,
                    u.user_sound_status,
                    s.school_name,
                    s.school_badge,
                    c.college_name,
                    u.user_signature,
                    u.user_praise 
                FROM
                    ( SELECT * FROM v_friends_eachother WHERE friends_user_id = :user_id ) AS f1
                    INNER JOIN t_user u ON u.user_id = f1.friends_target_user_id 
                    AND u.user_status <> '1'
                    INNER JOIN t_school s ON u.user_school_id = s.school_id 
                    AND s.school_status = '1'
                    INNER JOIN t_college c ON u.user_college_id = c.college_id 
                    AND c.college_status = '1'`;
        } else if (type === 2) {
            queryStr = `
        SELECT
            u.user_id,
            u.user_nickname,
            u.user_avatarUrl,
            u.user_sex,
            u.user_sound_url,
            u.user_sound_status,
            s.school_name,
            s.school_badge,
            c.college_name,
            u.user_signature,
            u.user_praise 
        FROM
            (
        SELECT
            * 
        FROM
            (
        SELECT
            f1.friends_user_id 
        FROM
            (
        SELECT
            * 
        FROM
            t_friends AS b 
        WHERE
            b.friends_target_user_id = :user_id  
            AND b.friends_relation = '2' 
            AND b.friends_status <> '3' 
            AND b.friends_id NOT IN ( SELECT friends_id FROM v_friends_eachother ) 
            ) AS f1
            INNER JOIN t_friends f ON f1.friends_user_id = f.friends_target_user_id 
            AND f1.friends_target_user_id = f.friends_user_id 
            AND f.friends_status <> '3' 
            ) temp1 UNION
            (
        SELECT
            f1.friends_user_id 
        FROM
            (
        SELECT
            * 
        FROM
            t_friends AS b 
        WHERE
            b.friends_target_user_id = :user_id 
            AND b.friends_relation = '2' 
            AND b.friends_status <> '3' 
            AND b.friends_id NOT IN ( SELECT friends_id FROM v_friends_eachother ) 
            ) AS f1
            LEFT JOIN t_friends f ON f1.friends_user_id = f.friends_target_user_id 
            AND f1.friends_target_user_id = f.friends_user_id 
        WHERE
            f.friends_id IS NULL 
            ) 
            ) temp
            INNER JOIN t_user u ON u.user_id = temp.friends_user_id 
            AND u.user_status <> '1' 
            AND u.user_auditing_status <> 3
            INNER JOIN t_school s ON u.user_school_id = s.school_id 
            AND s.school_status = '1'
            INNER JOIN t_college c ON u.user_college_id = c.college_id 
            AND c.college_status = '1'`;
        } else if (type === 3) {
            queryStr = `
        SELECT
            u.user_id,
            u.user_nickname,
            u.user_avatarUrl,
            u.user_sex,
            u.user_sound_url,
            u.user_sound_status,
            s.school_name,
            s.school_badge,
            c.college_name,
            u.user_signature,
            u.user_praise 
        FROM
            (
        SELECT
            * 
        FROM
            (
        SELECT
            f1.friends_target_user_id 
        FROM
            (
        SELECT
            * 
        FROM
            t_friends AS b 
        WHERE
            b.friends_user_id = :user_id 
            AND b.friends_relation = '2' 
            AND b.friends_status <> '3' 
            AND b.friends_id NOT IN ( SELECT friends_id FROM v_friends_eachother ) 
            ) AS f1
            INNER JOIN t_friends f ON f1.friends_user_id = f.friends_target_user_id 
            AND f1.friends_target_user_id = f.friends_user_id 
            AND f.friends_status <> '3' 
            ) temp1 UNION
            (
        SELECT
            f1.friends_target_user_id 
        FROM
            (
        SELECT
            * 
        FROM
            t_friends AS b 
        WHERE
            b.friends_user_id = :user_id 
            AND b.friends_relation = '2' 
            AND b.friends_status <> '3' 
            AND b.friends_id NOT IN ( SELECT friends_id FROM v_friends_eachother ) 
            ) AS f1
            LEFT JOIN t_friends f ON f1.friends_user_id = f.friends_target_user_id 
            AND f1.friends_target_user_id = f.friends_user_id 
        WHERE
            f.friends_id IS NULL 
            ) 
            ) temp
            INNER JOIN t_user u ON u.user_id = temp.friends_target_user_id 
            AND u.user_status <> '1' 
            AND u.user_auditing_status <> 3
            INNER JOIN t_school s ON u.user_school_id = s.school_id 
            AND s.school_status = '1'
            INNER JOIN t_college c ON u.user_college_id = c.college_id 
            AND c.college_status = '1'`;
        }else{
            throw new Error(JSON.stringify({code:2,msg:'无法识别的类型'}));
        }
        const result: any = await this.ctx.model.query(queryStr, {
            type: this.ctx.model.QueryTypes.SELECT,
            replacements: { user_id: param.user_id }
        })
        if (result.length <= 0) {
            throw new Error(JSON.stringify({ code: 2, msg: '没有数据' }));
        }
        let resultList: any = [];
        for (let i = 0; i < result.length; i++) {
            const item: any = {};
            item.user_id = result[i].user_id;
            item.user_nickname = result[i].user_nickname;
            item.user_avatarUrl = result[i].user_avatarUrl;
            item.user_sex = result[i].user_sex;
            item.user_sound_url = result[i].user_sound_url;
            item.user_sound_status = result[i].user_sound_status;
            item.school_name = result[i].school_name;
            item.school_badge = result[i].school_badge;
            item.college_name = result[i].college_name;
            item.user_signature = result[i].user_signature;
            item.user_praise = result[i].user_praise;
            let resultPraise = await this.ctx.model.Praise.findOne({ attributes: ['praise_id'], where: { user_id: param.user_id, target_id: item.user_id, is_delete: '1' } });
            if (!resultPraise) {
                item.is_praise = '2';
            } else {
                item.is_praise = '1';
            }
            const evaluateCount = await this.ctx.service.userService.getEvaluate(item.user_id);
            item.evaluateCount=evaluateCount;
            resultList.push(item);
        }
        const returnData = { count: result.length, userList: resultList };
        return returnData;
    }
}
export default FriendsService;